<?php
class ChuhuoApp extends MemberbaseApp {
    var $_role;
    function  __construct() {
        $this->ChuhuoApp();
    }
    function ChuhuoApp() {
        parent::__construct();
        $role = $this->visitor->get('role');
        $this->_role=$role;
        $user=$this->visitor->get();
        $this->user=$user;
        $this->assign('user',$user);
        $this->assign('role',$role);
    }
    function index(){
        $this->check_powers(array(USER_MANAGER,USER_CHUANWU));
        $order_mod=&m('order');
        $page=$_GET['page']?intval($_GET['page']):'1';
        $perpage=10;
        $start=($page-1)*$perpage;
        $start<0 && $start=0;
        $condition=$this->_get_conditions();
        $condition.=($condition?' and' :'').' submit_date >0 and send_date<1 ';
        $order_list=$order_mod->find(
                array(
                'limit'=>"$start,$perpage",
                'conditions'=>$condition
                )
        );
        $sql = "SELECT COUNT(*) FROM  {$order_mod->table} " . ($condition?" where ".$condition:'');
        $total_count = $order_mod->getOne($sql);
        $page = $this->_get_page($perpage);
        $page['item_count'] =$total_count;
        $this->_format_page($page);
        $this->assign('buyer_list',$order_mod->get_common_data('member'));
        if($_GET['search'] || $_GET['status'] =='1') {
            $this->assign('search', '1');
            $this->assign('get_para', ecm_json_encode($_GET));
        }
         $this->assign('factory_list', $order_mod->get_common_data('factory_info'));
         $this->assign('brand_list',$order_mod->get_common_data('brand'));
        $this->assign('page_info', $page);
        $this->assign('order_list', $order_list);
        $this->display('chuhuo.index.html');
    }
    function update(){
        $this->check_powers(array(USER_MANAGER,USER_CHUANWU));
        if($_POST['submit']){
            if(!$_POST['id']){
                $this->show_warning('请选择订单');
                exit;
            }
            $order_mod=&m('order');
            $order_mod->edit('order_id IN('.join(',',$_POST['id']).')',array('status'=>'3','send_date'=>TIME));
            $this->show_message('更新完成');
        }

    }
    function _get_conditions() {
        if($this->_role==USER_MERCHANDISER) {
            $user=$this->visitor->get();
            //print_r($user);
            $this->assign('merchandiser_list',array($user));
        }
        else {
            $user=&m('member')->find(array('conditions'=>'role='.USER_MERCHANDISER));
            //print_r($user);
            $this->assign('merchandiser_list',$user);
        }
       
        if($_GET['order_create_date']) {
            $conditions[]=" order_create_date >='".strtotime($_GET['order_create_date'])."' ";
        }
        if($_GET['order_create_date2']) {
            $conditions[]=" order_create_date <='".strtotime($_GET['order_create_date2 c '])."' ";
        }
        if($_GET['order_sn']) {
            $conditions[]=" order_sn like '%".$_GET['order_sn']."%' ";
        }
        if($_GET['style_sn']) {
            $conditions[]=" style_sn like '%".$_GET['style_sn']."%' ";
        }
        if($_GET['buyer_id']) {
            $conditions[]=" buyer_id='".intval($_GET['buyer_id'])."' ";
        }
        if($_GET['brand_id']) {
            $conditions[]=" brand_id='".intval($_GET['brand_id'])."' ";
        }
        if($_GET['style_id']) {
            $conditions[]=" style_id='".intval($_GET['style_id'])."' ";
        }
        if($_GET['special_id']) {
            $conditions[]=" special_id='".intval($_GET['special_id'])."' ";
        }
        if($_GET['feature_id']) {
            $conditions[]=" feature_id='".intval($_GET['feature_id'])."' ";
        }
        if($_GET['fabric_id']) {
            $conditions[]=" fabric_id='".intval($_GET['fabric_id'])."' ";
        }
        if($_GET['factory_id']) {
            $conditions[]=" factory_id='".intval($_GET['factory_id'])."' ";
        }
        if($_GET['merchandiser_id']>0) {
            $conditions[]=" merchandiser_id='".intval($_GET['merchandiser_id'])."' ";
        }
        elseif($this->_role==USER_MERCHANDISER && !isset($_GET['merchandiser_id'])) {
            $conditions[]=" merchandiser_id='".$user['user_id']."' ";
            //echo " merchandiser_id='".$user['user_id']."' ";
        }
        if($_GET['buyer_arrival_date']) {
            $conditions[]=" buyer_arrival_date>='".strtotime($_GET['buyer_arrival_date'])."' ";
        }
        if($_GET['buyer_arrival_date2']) {
            $conditions[]=" buyer_arrival_date<='".strtotime($_GET['buyer_arrival_date2'])."' ";
        }
        if($_GET['task']>0) {
            if($_GET['task']=='7days') {
                $weekday=date('N');
                $conditions[]=" buyer_arrival_date>='".(TIME-$weekday*3600*24)."' and buyer_arrival_date<=".(TIME+(7-$weekday)*3600*24)."";
            }
        }
        $condition=$conditions?join(' and ',$conditions):'';
        //echo $condition;
        return $condition;
    }
}

?>
